package com.jbit.redisserver.controller;

import com.jbit.common.RedisEntity;
import com.jbit.redisserver.utils.RedisUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/redis")
public class RedisController {
    @Resource
    private RedisUtils redisUtils;

    @RequestMapping("/set")
    public void set(@RequestBody RedisEntity redisEntity) {
        if (redisEntity.getExpire() == null || redisEntity.getExpire() <= 0){
            redisUtils.set(redisEntity.getKey(),redisEntity.getValue());
        }else{
            redisUtils.set(redisEntity.getKey(),redisEntity.getValue(),redisEntity.getExpire());
        }

    }
    @RequestMapping("/get")
    public String get(@RequestBody RedisEntity redisEntity){
        return redisUtils.get(redisEntity.getKey()).toString();
    }
    @RequestMapping("/exists")
    public boolean exists(@RequestBody RedisEntity redisEntity){
        return redisUtils.exists(redisEntity.getKey());
    }

}
